package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.gdpu.Service.MaintainingService;
import org.gdpu.Service.NewsService;
import org.gdpu.Service.StudentService;
import org.gdpu.Service.UserService;
import org.gdpu.model.*;
import org.gdpu.util.ChangeTime;
import org.gdpu.vo.MailboxVO;
import org.gdpu.vo.MaintainingVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 14-3-23.
 */
@Component("maintaining")
@Scope("prototype")
public class MaintainingAction extends MyActionSupport implements ModelDriven<MaintainingVO> {

    private MaintainingService maintainingService;
    private NewsService newsService;
    private UserService userService;
    private final MaintainingVO maintainingVO = new MaintainingVO();
    //获取当前登录用户
    HttpSession session = ServletActionContext.getRequest().getSession();
    User person = (User) session.getAttribute("person");

    public String sendMaintaining() {
        if (maintainingVO.getPhone().isEmpty()) {
            maintainingVO.setResultMessage("<script>alert('请输入联系电话！');location.href='/page/mailboxManagement/sendMail.jsp';</script>");
            return ERROR;
        }
        if (maintainingVO.getDescription().isEmpty()) {
            maintainingVO.setResultMessage("<script>alert('请输入维修描述！');location.href='/page/mailboxManagement/sendMail.jsp';</script>");
            return ERROR;
        }
        Maintaining maintaining = new Maintaining();
        maintaining.setPhone(maintainingVO.getPhone());
        maintaining.setDescription(maintainingVO.getDescription());
        maintaining.setDormitory(maintainingService.find(Dormitory.class, Integer.parseInt(maintainingVO.getDormitoryId())));
        maintaining.setApplyDate(ChangeTime.parseDate(ChangeTime.getCurrentDate()));
        maintaining.setState(MaintainingState.NOT_AUDIT);
        maintaining.setPreferTime(maintainingVO.getPreferTime());
        maintainingService.save(maintaining);
//
//        News news = newsService.createNews(person, userService.findByName("宿舍管理部"), "维修申请", "Maintaining!showOne.action?" + maintaining.getId());
//        if (news != null)
//            newsService.save(news);
        maintainingVO.setResultMessage("<script>alert('问题已反馈成功！我们会尽快处理！');location.href='/page/maintainingManagement/sendMaintaining.jsp';</script>");
        return SUCCESS;
    }

    public String show() {
        List<Maintaining> maintainings = maintainingService.findResultByDormitory(person.getDormitory());
        maintainingVO.setMaintainings(maintainings);
        view = "/page/maintainingManagement/showMaintaining.jsp";
        return VIEW;
    }

    public String showOne() {
        Maintaining maintaining = maintainingService.findResult(Integer.valueOf(maintainingVO.getId()));
        maintainingVO.setMaintaining(maintaining);
        if (maintainingVO.getFlag() == 1)
            view = "/page/maintainingManagement/maintaining.jsp";
        else
            view = "/page/maintainingManagement/adminMaintaining.jsp";
        return VIEW;
    }

    public String doMaintaining() {
        Maintaining maintaining = maintainingService.findResult(Integer.valueOf(maintainingVO.getId()));
        if (maintainingVO.getFlag() == 1) {
            maintaining.setEvaluate(maintainingVO.getEvaluate());
            maintaining.setSolveDate(new Date());
            maintaining.setState(MaintainingState.SOLVED);
            maintainingService.update(maintaining);
            maintainingVO.setResultMessage("<script>alert('评价成功！感谢您的参与！');location.href='Maintaining!show.action';</script>");
        } else {
            maintaining.setCheck(person);
            maintaining.setState(MaintainingState.AUDIT);
            maintainingService.update(maintaining);
            maintainingVO.setResultMessage("<script>alert('请尽快按照时间维修！谢谢!');location.href='Maintaining!adminShow.action';</script>");
        }
        return SUCCESS;
    }

    public String adminShow() {
        List<Maintaining> maintainings = maintainingService.findResult();
        maintainingVO.setMaintainings(maintainings);
        view = "/page/maintainingManagement/showAdminMaintaining.jsp";
        return VIEW;
    }


    @Resource(name = "maintainingServiceImpl")
    public void setMaintainingService(MaintainingService maintainingService) {
        this.maintainingService = maintainingService;
    }

    @Resource(name = "newsServiceImpl")
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    @Resource(name = "userServiceImpl")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Override
    public MaintainingVO getModel() {
        return maintainingVO;
    }
}
